﻿using MediatR;
using RedDragon.Common.Domain.Manufacture;
using RedDragon.Common.Domain.Process;
using RedDragon.Common.Infrastructure.Interface;
using System.Diagnostics;
using T.Common.Domain.Common;
using Process = RedDragon.Common.Domain.Process.Process;

namespace RedDragon.RBAC.API.Application.Common.ProcessW
{
    public class ProcessSearchHandler : IRequestHandler<ProcessSearchCommon, Result>
    {
        public readonly IBaseRepository<Process> ProcessRepository;
        public readonly IBaseRepository<WorkCenters> WorkCentersRepository;

        public ProcessSearchHandler(IBaseRepository<Process> processRepository, IBaseRepository<WorkCenters> workCentersRepository)
        {
            ProcessRepository = processRepository;
            WorkCentersRepository = workCentersRepository;
        }

        public async Task<Result> Handle(ProcessSearchCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = 0, Msg = "" };
            try
            {
                var list = from a in ProcessRepository.Query()
                           join
                           b in WorkCentersRepository.Query() 
                           on a.WorkCentreCode equals b.WorkCenterCode
                           select new ProcessDto()
                           {
                               ProcessId = a.ProcessId,
                               ProcessNumber = a.ProcessNumber,
                               ProcessName = a.ProcessName,
                               WorkCentreCode = a.WorkCentreCode,
                               WorkCenterName = b.WorkCenterName,
                               ProcessCreateTime = a.ProcessCreateTime,
                               ProcessCreateName = a.ProcessCreateName,
                               ProcessState = a.ProcessState,
                               ProcessStateName = a.ProcessState == true ? "有效" : "无效",
                               ProcessAudit = a.ProcessAudit,
                               ProcessAuditName = a.ProcessAudit == 0 ? "未提交" : a.ProcessAudit == 1 ? "已提交" : a.ProcessAudit == 2 ? "审核通过" : "已驳回",
                           };
                if (request.ProcessNumber != null)
                {
                    list = list.Where(x => x.ProcessNumber==request.ProcessNumber);
                }
                if (request.ProcessName != null)
                {
                    list = list.Where(x => x.ProcessName==request.ProcessName);
                }
                if (request.ProcessAudit != -1)
                {
                    list = list.Where(x => x.ProcessAudit == request.ProcessAudit);
                }
                result.Data = list;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
    public class ProcessDto
    {
        public int ProcessId { get; set; }
        /// <summary>
        /// 工序编码
        /// </summary>
        public string? ProcessNumber { get; set; }
        /// <summary>
        /// 工序名称
        /// </summary>
        public string? ProcessName { get; set; }
        /// <summary>
        /// 工作中心id
        /// </summary>
        public string WorkCentreCode { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime ProcessCreateTime { get; set; }
        /// <summary>
        /// 创建人
        /// </summary>
        public string? ProcessCreateName { get; set; }
        /// <summary>
        /// 状态
        /// </summary>
        public bool ProcessState { get; set; }
        /// <summary>
        /// 状态名称
        /// </summary>
        public string ProcessStateName { get; set; }
        /// <summary>
        /// 审批状态
        /// </summary>
        public int ProcessAudit { get; set; }
        /// <summary>
        /// 审批状态
        /// </summary>
        public string ProcessAuditName { get; set; }
        /// <summary>
        /// 工作中心名称
        /// </summary>
        public string WorkCenterName { get; set; }
    }
}
